package ph.com.isla.sf2d;

import ph.com.isla.sf2d.SF2DSpriteInterface.OnClickListener;
import ph.com.isla.sf2d.SF2DSpriteInterface.OnDragListener;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.RectF;

public abstract class SF2DSprite implements SF2DSpriteInterface.OnClickListener {
	private int id;
	private SF2DSurfaceView mSurface;
	private RectF mRectF;
	private Rect mRect;
	private Bitmap mBackground;
	private int mBackgroundColor;
	private OnClickListener mOnClickListener;
	private OnDragListener mOnDragListener;

	private boolean mClickable;
	private boolean mDraggable;
	private int status;

	public boolean isClickable() {
		return mClickable;
	}
	
	@Override
	public void onClick(final SF2DSprite sprite){
		if (sprite != this)
			return;
		
		new Thread(new Runnable() {
			@Override
			public void run() {
				animateClick();
			}
		}).start();
		
		
		if (mClickable) {
			new Thread(new Runnable() {
				@Override
				public void run() {
					getOnClickListener().onClick(sprite);
				}
			}).start();
		}
	}

	public void moveLeftTop(float x, float y) {
		float width = mRectF.width();
		float height = mRectF.height();
		mRectF.left = x;
		mRectF.top = y;
		mRectF.right = x + width;
		mRectF.bottom = y + height;
	}

	public void moveCenter(float centerX, float centerY) {
		float width = mRectF.width();
		float height = mRectF.height();

		mRectF.left = centerX - (width / 2);
		mRectF.top = centerY - (height / 2);
		mRectF.right = mRectF.left + width;
		mRectF.bottom = mRectF.top + height;
	}

	public void setOnClickListener(OnClickListener listener) {
		mOnClickListener = listener;
		if (listener != null)
			mClickable = true;
	}

	public void setOnDragListener(OnDragListener listener) {
		mOnDragListener = listener;
		if (listener != null)
			mDraggable = true;
	}

	public OnDragListener getOnDragListener() {
		return mOnDragListener;
	}

	public OnClickListener getOnClickListener() {
		return mOnClickListener;
	}

	public void setBackgroundBitmap(Bitmap bitmap) {
		mBackground = bitmap;
	}

	public Bitmap getBackgroundBitmap() {
		return mBackground;
	}

	public void setBackgroundColor(int color) {
		mBackgroundColor = color;
	}

	public int getBackgroundColor() {
		return mBackgroundColor;
	}

	public RectF getRectF() {
		return mRectF;
	}

	public SF2DSprite(RectF rectF) {
		this.mRectF = rectF;
		mRect = new Rect((int) mRectF.left, (int) mRectF.top,
				(int) mRectF.right, (int) mRectF.bottom);

		this.id = IDGenerator.generateId();
	}

	public int getId() {
		return this.id;
	}


	public abstract void draw(Canvas canvas);
	public abstract void animateClick();

	public boolean isDraggable() {
		return mDraggable;
	}

	public void setSurface(SF2DSurfaceView surface) {
		mSurface = surface;
	}

	public int getStatus() {
		return status;
	}

	public void setStatus(int status) {
		this.status = status;
	}

}
